using System;
using System.Collections.Generic;
using FPLibrary;
using UnityEngine;

namespace PathX.Unity
{
    partial class PathGridManager
    {
        private class EditorPathGridManager : IPathGridManager
        {
            private List<IGrid> gridList;

            public EditorPathGridManager(GameObject go)
            {
                gridList = new List<IGrid>(10);

                var grids = go.GetComponentsInChildren<PathGridComponent>();
                foreach (var grid in grids)
                {
                    grid.EnsureGrid();
                    
                    gridList.Add(grid.editorGrid);
                }
            }

            public IGrid GetGrid(FPVector pos)
            {
                foreach (var grid in gridList)
                {
                    if (grid.Contains(pos))
                        return grid;
                }

                return default;
            }

            public Cell GetCell(FPVector pos)
            {
                foreach (var grid in gridList)
                {
                    if (grid.Contains(pos))
                        return grid.GetCell(pos);
                }

                return default;
            }
        }
    }
}